import { ajax } from '../lib/ajax.js';
import { tools } from '../lib/tools.js';

//#region 上部分功能
let txt_title = document.getElementById('txt_title');
let txt_info = document.getElementById('txt_info');
let txt_username = document.getElementById('txt_username');
let btnQuery = document.getElementById('btnQuery');
let btnReset = document.getElementById('btnReset');
let btnNew = document.getElementById('btnNew');

let queryInfo = { title: '', content: '', username: '' };

// 查询
btnQuery.addEventListener('click', () => {
  page.pageNumber = 1;
  query();
});

// 重置
btnReset.addEventListener('click', () => {
  txt_title.value = '';
  txt_info.value = '';
  txt_username.value = '';
  page.pageNumber = 1;
  query();
});

// 发布
btnNew.addEventListener('click', () => {
  location.href = 'bbd_add.html';
});



//#endregion

//#region 获取数据query()
let list = [];
let page = {
  pageNumber: 1,
  pageSize: 10,
};

function query() {
  queryInfo.title = txt_title.value;
  queryInfo.content = txt_info.value;
  queryInfo.username = txt_username.value;

  ajax.get('/crud/billboard', tools.mergeJson(page,queryInfo), (data) => {
    if (!data.success) {
      alert(data.message);
      return;
    }
    // 数据处理
    list = data.data.data;
    page = data.data.pageBean;
    console.log('分页信息:', page);
    showData();
    showPage();
  });
}

//#endregion

//#region 获取tbody内容
let tbData = document.getElementById('tbData');
// 获取数据
function showData() {
  tbData.innerHTML = '';
  list.forEach((item) => {
    let tr = document.createElement('tr');
    let td;
    td = document.createElement('td');
    // 编号
    td = document.createElement('td');
    td.append(item.bid);
    tr.append(td);
    // 标题
    td = document.createElement('td');
    td.append(item.title);
    tr.append(td);
    // 发布人
    td = document.createElement('td');
    td.append(item.username);
    tr.append(td);
    // 是否置顶
    td = document.createElement('td');
    td.append(item.topIndex);
    tr.append(td);
    // 公告发布时间
    td = document.createElement('td');
    td.append(tools.formatDate(item.lastupdate));
    tr.append(td);

    tr.style.lineHeight = '3rem';
    tr.style.fontSize = '0.8rem';

    // 操作按钮
    // 查看内容
    td = document.createElement('td');
    let btnInfo = document.createElement('button');
    btnInfo.setAttribute('class', 'btn btn-success btn-sm');
    btnInfo.style.margin = '0.5rem';
    btnInfo.append('查看内容');
    btnInfo.addEventListener('click', () => {
      location.href = 'bbd_info.html?bid=' + item.bid;
    });
    td.append(btnInfo);
    // 修改
    let btnModi = document.createElement('button');
    btnModi.setAttribute('class', 'btn btn-warning btn-sm');
    btnModi.style.margin = '0.5rem';
    btnModi.append('修改');
    btnModi.addEventListener('click', () => {
      location.href = 'bbd_modi.html?bid=' + item.bid;
    });
    td.append(btnModi);
    // 删除
    let btnDel = document.createElement('button');
    btnDel.setAttribute('class', 'btn btn-danger btn-sm');
    btnDel.style.margin = '0.5rem';
    btnDel.append('删除');
    btnDel.addEventListener('click', () => {
      del(item);
    });
    td.append(btnDel);

    tr.append(td);

    tbData.append(tr);
  });
}

// 删除功能
function del(content) {
  console.log(content);
  if (!confirm(`是否删除:${content.title}?`)) {
    return;
  }
  ajax.delete(`/crud/billboard/${content.bid}`, {}, (data) => {
    alert(data.message);
    query();
  });
}

//#endregion



//#region 下部分功能
let spPre = document.getElementById('spPre');
let spPage = document.getElementById('spPage');
let spNext = document.getElementById('spNext');

function showPage(){
spPage.innerHTML=`当前页:${page.pageNumber}-总页数:${page.pageCount}`
} 

spNext.addEventListener('click', () => {
  page.pageNumber++;
  if (page.pageNumber > page.pageCount) {
    page.pageNumber = page.pageCount;
    return;
  }
  query();
});

spPre.addEventListener('click', () => {
  page.pageNumber--;
  // 不可以小于1
  if (page.pageNumber < 1) {
    page.pageNumber = 1;
    return;
  }
  query();
});

//#endregion

query();
